package com.example.exam.sysDictInfo.dao;

import com.example.exam.db.BaseDao;
import com.example.exam.db.PageInfoBean;
import com.example.exam.sysDictInfo.model.SysDictInfoBean;
import com.example.exam.sysRole.model.SysRoleBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.*;

@Repository
public class SysDictInfoDao extends BaseDao<SysDictInfoBean, Integer> {
    @Autowired
    JdbcTemplate jtl;
    @Override
    /**
     * @Description:查询字典表中的一条数据
     * @Param: []
     * @return: java.lang.String
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public String findByIdSql() {
             return "select * from sys_dict_info where id=?";
    }
    /**
     * @Description:获取字典列表页面的列表页面
     * @Param: [pageInfo, name]
     * @return: com.example.exam.db.PageInfoBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public PageInfoBean getListByTitle(PageInfoBean pageInfo, String name) {
        try {
            Map<String, Object> map = new HashMap<>();
            String sql = "select * from sys_dict_info where state=1 ";
            if(StringUtils.isNotEmpty(name)){
                sql += " and name like :name ";
                map.put("name", "%"+name+"%");
            }
            return this.pageForMap(sql, map, pageInfo);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }/**
     * @Description:查询子级字典
     * @Param: [id]
     * @return: java.util.List<com.example.exam.sysDictInfo.model.SysDictInfoBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<SysDictInfoBean> queryChild(Integer id) {
        try {
            String sql = "select * from sys_dict_info where state=1 and pid=1";
            return getJtl().query(sql,new BeanPropertyRowMapper<SysDictInfoBean>(SysDictInfoBean.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
        /**
            * @Description:添加字典信息
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getAddSql(){
        return "insert into sys_dict_info (id,pid, name, mark, `describe`, state, create_user, create_time) values (:id,:pid, :name, :mark, :describe, :state, :create_user, :create_time)";
    }
    @Override
        /**
            * @Description:删除字典信息
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getDeleteByIdSql(){
        return "delete from sys_dict_info where id = ? ";
    }
    @Override
        /**
            * @Description:修改字典信息
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getUpdateAllFieldSql(){
        return "update sys_dict_info set pid=:pid,name=:name, mark=:mark, `describe`=:describe where id=:id ";
    }
    /**
     * @Description:查询父级字典
     * @Param: []
     * @return: java.util.List
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List findParentDict(){
        String sql="SELECT *  FROM  sys_dict_info WHERE state=1 AND pid=0";
        List<SysDictInfoBean> list=getJtl().query(sql,new BeanPropertyRowMapper<SysDictInfoBean>(SysDictInfoBean.class));
        return list;
    }
}
